<?php

/**
 * WeEngine System
 *
 * (c) We7Team 2021 <https://www.w7.cc>
 *
 * This is not a free software
 * Using it under the license terms
 * visited https://www.w7.cc for more details
 */

namespace app\controller;

use app\model\TagModel;
use app\validate\TagValidate;
use Itwmw\Validate\Attributes\Validator;
use think\Request;

class Tag
{
    public function list(Request $request)
    {
        $tag    = new TagModel();
        $result = $tag->order('id', 'desc')->select()->toArray();
        return json_success(data:$result);
    }

    #[Validator(validate: TagValidate::class, fields: ['name'])]
    public function add(Request $request)
    {
        $name = get_validate_data($request)->get('name');

        TagModel::create([
            'name' => $name
        ]);

        return json_success('添加成功');
    }

    #[Validator(validate: TagValidate::class, fields: ['id'])]
    public function delete(Request $request)
    {
        $id = get_validate_data($request)->get('id');

        $tag    = new TagModel();
        $tag->find($id)->delete();

        return json_success('删除成功');
    }
}
